package com.ymatou.iapi.fastturnover.testcase;

import java.io.IOException;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import com.ymatou.iapi.fastturnover.parameter.addBean;
import com.ymatou.iapi.fastturnover.service.addCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.FastTurnoverService;
import com.ymttest.database.model.fastturnover.account_config_log;
import com.ymttest.database.model.fastturnover.account_config_logExample;
import com.ymttest.database.sqlwapper.fastturnover.account_config_logWrapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
/**
*添加配置
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_add{
	private static addBean bean;
	private static addCall call;
	//Long fastturnover_seller=(Long)Integer.parseInt(EnvSetup.getData("fastturnover_seller"));
	
	Long fastturnover_seller=Long.parseLong(EnvSetup.getData("fastturnover_seller"));
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("添加配置");
	}

	@Before
	public void caseUp() {
		bean=new addBean();
		call=new addCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() throws ClientProtocolException, IOException {
		FastTurnoverService.close(fastturnover_seller, bean.getOperator());
		Logger.end();
		
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_add_001() {
		Logger.start(true,"添加配置");
		try {
           //同一个买手可以配置多条记录，但是每条记录的生效时间段不能有重合（即同一时间，一个买手只能有一条生效的记录）,否则toast提示：在生效时间段内，该买手已经存在一条记录，不能重复配置！
			bean.setSellerId(fastturnover_seller);
			bean.setNikeName("自动化测试昵称_"+TestDataManager.getRandomIntNum(5));
			bean.setRate(1);
			bean.setRate(20);
			bean.setMaxLoan(true);
			bean.setOperator("autoTest");
		
			SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
			java.util.Date start=formatter.parse(LocalDateTime.now().plusDays(2).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
			java.util.Date end=formatter.parse(LocalDateTime.now().plusDays(3).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
			
			bean.setStartTime(start);
			bean.setEndTime(end);
			call.setData(bean);
			call.callService();
			
			account_config_logExample example=new account_config_logExample();
			example.createCriteria().andSeller_idEqualTo(bean.getSellerId());
			example.setOrderByClause(" id desc");
			
			account_config_logWrapper logWapper=new account_config_logWrapper();
			List<account_config_log> logs=logWapper.selectByExample(example);
			
			Logger.verifyEquals("200", call.getCode1(), "验证code");
			Logger.verifyEquals("true", call.getResult(), "验证result");
			Logger.verifyEquals(true, logs.size()>0, "验证数据库新增一条数据");
			
			if(logs.size()>0)
			{
				account_config_log log=logs.get(0);
				Logger.verifyEquals(log.getSeller_id(), bean.getSellerId(), "验证sellerId");
				
				Logger.verifyEquals(log.getNike_name(), bean.getNikeName(), "验证NikeName");
				Logger.verifyEquals(log.getSeller_id(), bean.getSellerId(), "验证sellerId");
				Logger.verifyEquals(log.getRate(), bean.getRate(), "验证ate");
				Logger.verifyEquals(log.getMax_loan(), bean.getMaxLoan(), "验证MaxLoan");
				//时间
				Logger.verifyEquals(formatter.format(log.getAdd_time()), formatter.format(bean.getStartTime()), "验证StartTime");
				Logger.verifyEquals(formatter.format(log.getEnd_time()), formatter.format(bean.getEndTime()), "验证EndTime");
				
				Logger.verifyEquals(log.getOperator(), bean.getOperator(), "验证Operator");
				Logger.verifyEquals(log.getAction_type(), 1, "验证action_type1，新增，2修改，3关闭");
				
			}
		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}